#include "SDLClipboard.h"

SDLClipboard::SDLClipboard() { }
SDLClipboard::~SDLClipboard() { }

std::string SDLClipboard::getData() {
	HGLOBAL hCBData = 0;
	LPTSTR lptstrData = 0;
	std::string ret;
	if(!OpenClipboard(NULL)) {
		//couldn't open the clipboard
		return NULL;
	}

	if(!IsClipboardFormatAvailable(CF_TEXT)) {
		//No text on the clipboard
		return NULL;
	}

	hCBData = GetClipboardData(CF_TEXT);  //get handle to clipboard data

	if(hCBData != NULL) {
		lptstrData = (LPTSTR)GlobalLock(hCBData);
		ret = (char*)lptstrData;
		GlobalUnlock(hCBData);
	}

	CloseClipboard();

	if(ret.size() > 0) return ret;
	else return NULL;
}

void SDLClipboard::setData(std::string data) {
	HGLOBAL hMem = 0;
	LPTSTR lptstrData = 0;
	
	if(!(data.size() > 0)) return;

	if(!OpenClipboard(NULL)) {
		//couldn't open the clipboard
		return;
	}

	EmptyClipboard();

	hMem = GlobalAlloc(GMEM_MOVEABLE, data.size() + 1);
	lptstrData = (TCHAR*)GlobalLock(hMem);
	memcpy(lptstrData, data.c_str(), data.size() + 1);
	GlobalUnlock(hMem);
	SetClipboardData(CF_TEXT, hMem);
	CloseClipboard();
}


